package cn.stylefeng.guns.modular.noticeMsg.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.noticeMsg.entity.NoticeMsg;
import cn.stylefeng.guns.modular.noticeMsg.mapper.NoticeMsgMapper;
import cn.stylefeng.guns.modular.noticeMsg.model.params.NoticeMsgParam;
import cn.stylefeng.guns.modular.noticeMsg.model.result.NoticeMsgResult;
import  cn.stylefeng.guns.modular.noticeMsg.service.NoticeMsgService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 通知消息 服务实现类
 * </p>
 *
 * @author lauyang
 * @since 2020-04-18
 */
@Service
public class NoticeMsgServiceImpl extends ServiceImpl<NoticeMsgMapper, NoticeMsg> implements NoticeMsgService {

    @Override
    public void add(NoticeMsgParam param){
        NoticeMsg entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NoticeMsgParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NoticeMsgParam param){
        NoticeMsg oldEntity = getOldEntity(param);
        NoticeMsg newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NoticeMsgResult findBySpec(NoticeMsgParam param){
        return null;
    }

    @Override
    public List<NoticeMsgResult> findListBySpec(NoticeMsgParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NoticeMsgParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NoticeMsgParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NoticeMsg getOldEntity(NoticeMsgParam param) {
        return this.getById(getKey(param));
    }

    private NoticeMsg getEntity(NoticeMsgParam param) {
        NoticeMsg entity = new NoticeMsg();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
